package cn.hrlly.tao2shou.utils;

import android.content.Context;

import com.google.gson.Gson;
import com.nostra13.universalimageloader.utils.StorageUtils;

import java.io.File;

import cn.hrlly.tao2shou.other.Config;
import cn.hrlly.tao2shou.domain.ApplicationList;
import cn.hrlly.tao2shou.domain.CommodityList;
import cn.hrlly.tao2shou.domain.OrderList;
import cn.hrlly.tao2shou.domain.RequireList;

/**
 * 常用缓存工具类，包括缓存发布商品、发布求购、商品展示
 * 求购展示、买家申请、卖家申请、买家待完成订单、买家已完成订单
 * 卖家待完成订单、卖家已完成订单
 * 以上缓存全部存储在文件中，保存时间为15天，15天后自动失效
 * User: gengliang
 * Date: 2016-01-20
 * Time: 14:46
 */

public class CacheUtil {

    private ACache aCache;

    public CacheUtil(Context context) {
        //缓存路径
        File file = StorageUtils.getOwnCacheDirectory(context, "Tao2Shou");
        aCache = ACache.get(file);
    }

    //缓存发布商品
    public void putPublishCommodity(CommodityList commodityList) {
        String string = new Gson().toJson(commodityList);
        aCache.put(Config.key_publish_commodity, string, 15 * ACache.TIME_DAY);
    }

    //缓存发布求购
    public void putPublishRequire(RequireList requireList) {
        String string = new Gson().toJson(requireList);
        aCache.put(Config.key_publish_Require, string, 15 * ACache.TIME_DAY);
    }

    //缓存商品展示
    public void putCommodityList(CommodityList commodityList) {
        String string = new Gson().toJson(commodityList);
        aCache.put(Config.key_commodity_list, string, 15 * ACache.TIME_DAY);
    }

    //缓存求购展示
    public void putRequireList(RequireList requireList) {
        String string = new Gson().toJson(requireList);
        aCache.put(Config.key_require_list, string, 15 * ACache.TIME_DAY);
    }

    //缓存买家申请
    public void putBuyerApplication(ApplicationList applicationList) {
        String string = new Gson().toJson(applicationList);
        aCache.put(Config.key_buyer_application, string, 15 * ACache.TIME_DAY);
    }

    //缓存卖家申请
    public void putSellerApplication(ApplicationList applicationList) {
        String string = new Gson().toJson(applicationList);
        aCache.put(Config.key_seller_application, string, 15 * ACache.TIME_DAY);
    }

    //缓存买家待完成订单
    public void putBuyerUnfinishOrder(OrderList orderList) {
        String string = new Gson().toJson(orderList);
        aCache.put(Config.key_buyer_unfinish_order, string, 15 * ACache.TIME_DAY);
    }

    //缓存买家已完成订单
    public void putBuyerCompleteOrder(OrderList orderList) {
        String string = new Gson().toJson(orderList);
        aCache.put(Config.key_buyer_complete_order, string, 15 * ACache.TIME_DAY);
    }

    //缓存卖家待完成订单
    public void putSellerUnfinishOrder(OrderList orderList) {
        String string = new Gson().toJson(orderList);
        aCache.put(Config.key_seller_unfinish_order, string, 15 * ACache.TIME_DAY);
    }

    //缓存卖家已完成订单
    public void putSellerCompleteOrder(OrderList orderList) {
        String string = new Gson().toJson(orderList);
        aCache.put(Config.key_seller_compelte_order, string, 15 * ACache.TIME_DAY);
    }

    //获取发布商品
    public CommodityList getPublishCommodity() {
        String string = aCache.getAsString(Config.key_publish_commodity);
        if (string==null) {
            return null;
        } else {
            CommodityList commodityList = new Gson().fromJson(string, CommodityList.class);
            return commodityList;
        }
    }

    //获取发布求购
    public RequireList getPublishRequire() {
        String string = aCache.getAsString(Config.key_publish_Require);
        if (string==null) {
            return null;
        } else {
            RequireList requireList = new Gson().fromJson(string, RequireList.class);
            return requireList;
        }
    }

    //获取商品展示
    public CommodityList getCommodityList() {
        String string = aCache.getAsString(Config.key_commodity_list);
        if (string==null) {
            return null;
        } else {
            CommodityList commodityList = new Gson().fromJson(string, CommodityList.class);
            return commodityList;
        }
    }

    //获取求购展示
    public RequireList getRequireList() {
        String string = aCache.getAsString(Config.key_require_list);
        if (string==null) {
            return null;
        } else {
            RequireList requireList = new Gson().fromJson(string, RequireList.class);
            return requireList;
        }
    }

    //获取买家申请
    public ApplicationList getBuyerApplication() {
        String string = aCache.getAsString(Config.key_buyer_application);
        if (string==null) {
            System.out.println("123456");
            return null;
        } else {
            ApplicationList applicationList = new Gson().fromJson(string, ApplicationList.class);
            return applicationList;
        }
    }

    //获取卖家申请
    public ApplicationList getSellerApplication() {
        String string = aCache.getAsString(Config.key_seller_application);
        if (string==null) {
            return null;
        }else {
            ApplicationList applicationList = new Gson().fromJson(string, ApplicationList.class);
            return applicationList;
        }
    }

    //获取买家待完成订单
    public OrderList getBuyerUnfinishOrder() {
        String string = aCache.getAsString(Config.key_buyer_unfinish_order);
        if (string==null) {
            return null;
        }else {
            OrderList orderList = new Gson().fromJson(string, OrderList.class);
            return orderList;
        }
    }

    //获取买家已完成订单
    public OrderList getBuyerCompleteOrder() {
        String string = aCache.getAsString(Config.key_buyer_complete_order);
        if (string==null) {
            return null;
        }else {
            OrderList orderList = new Gson().fromJson(string, OrderList.class);
            return orderList;
        }
    }

    //获取卖家待完成订单
    public OrderList getSellerUnfinishOrder() {
        String string = aCache.getAsString(Config.key_seller_unfinish_order);
        if (string==null) {
            return null;
        } else {
            OrderList orderList = new Gson().fromJson(string, OrderList.class);
            return orderList;
        }
    }

    //获取卖家已完成订单
    public OrderList getSellerCompleteOrder() {
        String string = aCache.getAsString(Config.key_seller_compelte_order);
        if (string==null) {
            return null;
        } else {
            OrderList orderList = new Gson().fromJson(string, OrderList.class);
            return orderList;
        }
    }

}
